{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "77b72226",
   "metadata": {},
   "source": [
    "### 椭圆各种的花式关系让直线过定点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c65e878f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3b5ab595",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/home/huang/python_code',\n",
       " '/home/huang/anaconda3/lib/python39.zip',\n",
       " '/home/huang/anaconda3/lib/python3.9',\n",
       " '/home/huang/anaconda3/lib/python3.9/lib-dynload',\n",
       " '',\n",
       " '/home/huang/anaconda3/lib/python3.9/site-packages']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys\n",
    "sys.path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fb10a6b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.append(\"/home/huang/Documents/packaging_tutorial/src\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "54855790",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/home/huang/python_code',\n",
       " '/home/huang/anaconda3/lib/python39.zip',\n",
       " '/home/huang/anaconda3/lib/python3.9',\n",
       " '/home/huang/anaconda3/lib/python3.9/lib-dynload',\n",
       " '',\n",
       " '/home/huang/anaconda3/lib/python3.9/site-packages',\n",
       " '/home/huang/Documents/packaging_tutorial',\n",
       " '/home/huang/Documents/packaging_tutorial/src']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sys.path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7d24fa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "62924010",
   "metadata": {},
   "outputs": [],
   "source": [
    "%autoreload 1\n",
    "%aimport quadratic_function.ellipse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "54e8059a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from quadratic_function.ellipse import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "64109bcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{y^{2}}{b^{2}} + \\frac{x^{2}}{a^{2}} = 1$"
      ],
      "text/plain": [
       "Eq(y**2/b**2 + x**2/a**2, 1)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ellipse_function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b270d25f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{a^{2} - b^{2}}$"
      ],
      "text/plain": [
       "sqrt(a**2 - b**2)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f0f0e00b",
   "metadata": {},
   "outputs": [],
   "source": [
    "m=symbols(\"m\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "277b9527",
   "metadata": {},
   "outputs": [],
   "source": [
    "line_F_equation=Eq(m*y,x+c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ac17439d",
   "metadata": {},
   "outputs": [],
   "source": [
    "M,N=list(nonlinsolve([line_F_equation,ellipse_function],[x,y]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "93069eb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left( m \\left(\\frac{a b^{2} \\sqrt{m^{2} + 1}}{a^{2} + b^{2} m^{2}} + \\frac{b^{2} m \\sqrt{a - b} \\sqrt{a + b}}{a^{2} + b^{2} m^{2}}\\right) - \\sqrt{a^{2} - b^{2}}, \\  \\frac{a b^{2} \\sqrt{m^{2} + 1}}{a^{2} + b^{2} m^{2}} + \\frac{b^{2} m \\sqrt{a - b} \\sqrt{a + b}}{a^{2} + b^{2} m^{2}}\\right)$"
      ],
      "text/plain": [
       "(m*(a*b**2*sqrt(m**2 + 1)/(a**2 + b**2*m**2) + b**2*m*sqrt(a - b)*sqrt(a + b)/(a**2 + b**2*m**2)) - sqrt(a**2 - b**2), a*b**2*sqrt(m**2 + 1)/(a**2 + b**2*m**2) + b**2*m*sqrt(a - b)*sqrt(a + b)/(a**2 + b**2*m**2))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "cc3d71ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left( m \\left(- \\frac{a b^{2} \\sqrt{m^{2} + 1}}{a^{2} + b^{2} m^{2}} + \\frac{b^{2} m \\sqrt{a - b} \\sqrt{a + b}}{a^{2} + b^{2} m^{2}}\\right) - \\sqrt{a^{2} - b^{2}}, \\  - \\frac{a b^{2} \\sqrt{m^{2} + 1}}{a^{2} + b^{2} m^{2}} + \\frac{b^{2} m \\sqrt{a - b} \\sqrt{a + b}}{a^{2} + b^{2} m^{2}}\\right)$"
      ],
      "text/plain": [
       "(m*(-a*b**2*sqrt(m**2 + 1)/(a**2 + b**2*m**2) + b**2*m*sqrt(a - b)*sqrt(a + b)/(a**2 + b**2*m**2)) - sqrt(a**2 - b**2), -a*b**2*sqrt(m**2 + 1)/(a**2 + b**2*m**2) + b**2*m*sqrt(a - b)*sqrt(a + b)/(a**2 + b**2*m**2))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "N"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "57265a6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "E=(-a**2/c,M[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "dd30a184",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-a**2/sqrt(a**2 - b**2),\n",
       " a*b**2*sqrt(m**2 + 1)/(a**2 + b**2*m**2) + b**2*m*sqrt(a - b)*sqrt(a + b)/(a**2 + b**2*m**2))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "E"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "edc30e65",
   "metadata": {},
   "outputs": [],
   "source": [
    "line_EN_equation=Eq((y-N[1])*(E[0]-N[0]),(x-N[0])*(E[1]-N[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "901563b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "answer=list(solveset(line_EN_equation.subs(y,0),x))[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7e733974",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{- a^{2} + \\frac{b^{2}}{2}}{\\sqrt{a^{2} - b^{2}}}$"
      ],
      "text/plain": [
       "(-a**2 + b**2/2)/sqrt(a**2 - b**2)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(answer)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49c82113",
   "metadata": {},
   "source": [
    "#### 过定点的直线特点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "76f5dbae",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_0,y_0=symbols(\"x_0,y_0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "51a5269b",
   "metadata": {},
   "outputs": [],
   "source": [
    "k,t=symbols(\"k,t\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "2ec32ff9",
   "metadata": {},
   "outputs": [],
   "source": [
    "P=(x_0,y_0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "16b17fbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "line_AB_equation=Eq(y,k*x+t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "43a433a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "A,B=list(nonlinsolve([line_AB_equation,ellipse_function],[x,y]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d80683f8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{2 a^{2} k t}{a^{2} k^{2} + b^{2}}$"
      ],
      "text/plain": [
       "-2*a**2*k*t/(a**2*k**2 + b**2)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(A[0]+B[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "d28ff03e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{2 b^{2} t}{a^{2} k^{2} + b^{2}}$"
      ],
      "text/plain": [
       "2*b**2*t/(a**2*k**2 + b**2)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(A[1]+B[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "9b6b804e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{a^{2} \\left(- b^{2} + t^{2}\\right)}{a^{2} k^{2} + b^{2}}$"
      ],
      "text/plain": [
       "a**2*(-b**2 + t**2)/(a**2*k**2 + b**2)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(A[0]*B[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f0fce07",
   "metadata": {},
   "source": [
    "#### 过定点的直线与椭圆上一点组成的问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "18dce23d",
   "metadata": {},
   "outputs": [],
   "source": [
    "d=symbols(\"d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "4fcb5f09",
   "metadata": {},
   "outputs": [],
   "source": [
    "line_d_equation=Eq(y,k*(x-d))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9ad6cf82",
   "metadata": {},
   "outputs": [],
   "source": [
    "A,B=list(nonlinsolve([line_d_equation,ellipse_function],[x,y]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "78646108",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left( \\frac{\\frac{a b k \\sqrt{a^{2} k^{2} + b^{2} - d^{2} k^{2}}}{a^{2} k^{2} + b^{2}} - \\frac{b^{2} d k}{a^{2} k^{2} + b^{2}} + d k}{k}, \\  \\frac{a b k \\sqrt{a^{2} k^{2} + b^{2} - d^{2} k^{2}}}{a^{2} k^{2} + b^{2}} - \\frac{b^{2} d k}{a^{2} k^{2} + b^{2}}\\right)$"
      ],
      "text/plain": [
       "((a*b*k*sqrt(a**2*k**2 + b**2 - d**2*k**2)/(a**2*k**2 + b**2) - b**2*d*k/(a**2*k**2 + b**2) + d*k)/k, a*b*k*sqrt(a**2*k**2 + b**2 - d**2*k**2)/(a**2*k**2 + b**2) - b**2*d*k/(a**2*k**2 + b**2))"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "bb843a4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left( \\frac{- \\frac{a b k \\sqrt{a^{2} k^{2} + b^{2} - d^{2} k^{2}}}{a^{2} k^{2} + b^{2}} - \\frac{b^{2} d k}{a^{2} k^{2} + b^{2}} + d k}{k}, \\  - \\frac{a b k \\sqrt{a^{2} k^{2} + b^{2} - d^{2} k^{2}}}{a^{2} k^{2} + b^{2}} - \\frac{b^{2} d k}{a^{2} k^{2} + b^{2}}\\right)$"
      ],
      "text/plain": [
       "((-a*b*k*sqrt(a**2*k**2 + b**2 - d**2*k**2)/(a**2*k**2 + b**2) - b**2*d*k/(a**2*k**2 + b**2) + d*k)/k, -a*b*k*sqrt(a**2*k**2 + b**2 - d**2*k**2)/(a**2*k**2 + b**2) - b**2*d*k/(a**2*k**2 + b**2))"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "67eff8a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{2 a^{2} d k^{2}}{a^{2} k^{2} + b^{2}}$"
      ],
      "text/plain": [
       "2*a**2*d*k**2/(a**2*k**2 + b**2)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(A[0]+B[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "9d06636c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{2 b^{2} d k}{a^{2} k^{2} + b^{2}}$"
      ],
      "text/plain": [
       "-2*b**2*d*k/(a**2*k**2 + b**2)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(A[1]+B[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "11822a9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{a^{2} \\left(- b^{2} + d^{2} k^{2}\\right)}{a^{2} k^{2} + b^{2}}$"
      ],
      "text/plain": [
       "a**2*(-b**2 + d**2*k**2)/(a**2*k**2 + b**2)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(A[0]*B[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "c587242b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{b^{2} k^{2} \\left(- a^{2} + d^{2}\\right)}{a^{2} k^{2} + b^{2}}$"
      ],
      "text/plain": [
       "b**2*k**2*(-a**2 + d**2)/(a**2*k**2 + b**2)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(A[1]*B[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "f6ac6430",
   "metadata": {},
   "outputs": [],
   "source": [
    "C=(x_0,y_0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "b05c6ed0",
   "metadata": {},
   "outputs": [],
   "source": [
    "%aimport quadratic_function.ellipse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "2d95486e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from basic_package.basic import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "46c25174",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '_distance_point' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [68]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# test whether function _distance_point can be used.\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43m_distance_point\u001b[49m(point_A,point_B)\n",
      "\u001b[0;31mNameError\u001b[0m: name '_distance_point' is not defined"
     ]
    }
   ],
   "source": [
    "# test whether function _distance_point can be used.\n",
    "_distance_point(point_A,point_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "512c1e9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "point_A=Point(A)\n",
    "point_B=Point(B)\n",
    "point_C=Point(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3bccc342",
   "metadata": {},
   "outputs": [],
   "source": [
    "answer=simplify((point_A-point_C).dot(point_B-point_C))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "df3f4d4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "final=cancel(answer.subs([(a,sqrt(6)),(b,sqrt(3)),(d,1),(x_0,-2),(y_0,1)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "0715592e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def quadratic_equation_delt(eq,eq_x):\n",
    "    # first we should check if eq is quadratic equation\n",
    "    # not complement yet\n",
    "    expr=simplify(eq.lhs-eq.rhs)\n",
    "    sums=expr.subs(eq_x,1)\n",
    "    c=simplify(expr.subs(eq_x,0))\n",
    "    b=simplify((sums-expr.subs(eq_x,-1))/2)\n",
    "    a=sums-b-c\n",
    "    return simplify(b**2-4*a*c)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "7308ec1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_eq=Eq(fraction(final)[0]-fraction(final)[1]*m,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "74f09c02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - 4 \\cdot \\left(2 m^{2} - 13 m - 16\\right)$"
      ],
      "text/plain": [
       "-4*(2*m**2 - 13*m - 16)"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(quadratic_equation_delt(final_eq,k))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "2cd1c9c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "k=symbols(\"k\",real=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "0c6b749c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k in Reals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "fe4c80ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "check_eq=Eq((k**2+Rational(1,12))*x**2+k*x-Rational(3,4),0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "9f223d89",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_1,x_2=list(solveset(check_eq,x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "d53d2d6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{6 k}{12 k^{2} + 1} - \\frac{3 \\sqrt{16 k^{2} + 1}}{12 k^{2} + 1}$"
      ],
      "text/plain": [
       "-6*k/(12*k**2 + 1) - 3*sqrt(16*k**2 + 1)/(12*k**2 + 1)"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "d854a114",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{6 k}{12 k^{2} + 1} + \\frac{3 \\sqrt{16 k^{2} + 1}}{12 k^{2} + 1}$"
      ],
      "text/plain": [
       "-6*k/(12*k**2 + 1) + 3*sqrt(16*k**2 + 1)/(12*k**2 + 1)"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "82c56b24",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_expr=simplify(abs((x_1-x_2)/((2*k+1)**2*x_1*x_2-(2*k+1)*(x_1+x_2)+1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "fc1ad704",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{3 \\sqrt{16 k^{2} + 1}}{4 \\left|{3 k + 1}\\right|}$"
      ],
      "text/plain": [
       "3*sqrt(16*k**2 + 1)/(4*Abs(3*k + 1))"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "cddab9cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "expr_3=(final_expr*4/3)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "461e68c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{16 k^{2} + 1}{\\left(3 k + 1\\right)^{2}}$"
      ],
      "text/plain": [
       "(16*k**2 + 1)/(3*k + 1)**2"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(expr_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "ac8e7a78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{\\frac{3}{16}\\right\\}$"
      ],
      "text/plain": [
       "{3/16}"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solveset(diff(simplify(expr_3),k),k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "c1133a6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{16}{25}$"
      ],
      "text/plain": [
       "16/25"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(expr_3).subs(k,Rational(3,16))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "cdf79ab2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{2}$"
      ],
      "text/plain": [
       "sqrt(2)"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Abs(1+I)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
